﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ADODB;
using CDO;


namespace Common
{
    public class ExchangeSendMail
    {
        #region 普通邮件
        /// <summary>
        /// 普通邮件
        /// </summary>
        /// <param name="from">发件人</param>
        /// <param name="username">发件人姓名</param>
        /// <param name="to">收件人</param>
        /// <param name="title">标题</param>
        /// <param name="body">内容</param>
        public static void SendNoFile(string from, string username, string to, string title, string body)
        {
            CDO.Message msg = new CDO.Message();

            msg.BodyPart.Charset = "gb2312";
            msg.Sender = username;
            msg.From = from;
            msg.To = to;
            msg.Subject = title;
            //msg.TextBody = body;
            msg.HTMLBody = body;    //html文本
            OverAll(msg, from);
        }
        #endregion

        #region 带抄送
        /// <summary>
        /// 带抄送
        /// </summary>
        /// <param name="from">发件人</param>
        /// <param name="username">发件人姓名</param>
        /// <param name="to">收件人</param>
        /// <param name="cc">抄送</param>
        /// <param name="title">标题</param>
        /// <param name="body">内容</param>
        public static void SendNoFile(string from, string username, string to, string cc, string title, string body)
        {
            CDO.Message msg = new CDO.Message();
            msg.BodyPart.Charset = "gb2312";
            msg.Sender = username;
            msg.From = from;
            msg.To = to;
            msg.Subject = title;
            //msg.TextBody = body;
            msg.HTMLBody = body;    //html文本
            msg.CC = cc;
            OverAll(msg, from);
        }
        #endregion

        #region 带附件
        /// <summary>
        /// 带附件
        /// </summary>
        /// <param name="from">发件人</param>
        /// <param name="username">发件人姓名</param>
        /// <param name="to">收件人</param>
        /// <param name="title">标题</param>
        /// <param name="body">内容</param>
        /// <param name="file">附件</param>
        public static void SendFile(string from, string username, string to, string title, string body, string file)
        {
            CDO.Message msg = new CDO.Message();
            msg.BodyPart.Charset = "gb2312";
            msg.Sender = username;
            msg.From = from;
            msg.To = to;
            msg.Subject = title;
            //msg.TextBody = body; //纯文本
            msg.HTMLBody = body;    //html文本
            msg.AddAttachment(file);
            OverAll(msg, from);
        }

        #endregion

        #region 带附件带抄送
        /// <summary>
        /// 带附件带抄送
        /// </summary>
        /// <param name="from">发件人地址</param>
        /// <param name="username">发件人姓名</param>
        /// <param name="to">收件人</param>
        /// <param name="cc">抄送</param>
        /// <param name="title">标题</param>
        /// <param name="body">内容</param>
        /// <param name="file">附件</param>
        public static void SendFile(string from, string username, string to, string cc, string title, string body, string file)
        {
            CDO.Message msg = new CDO.Message();
            msg.BodyPart.Charset = "gb2312";
            msg.Sender = username;
            msg.From = from;
            msg.To = to;
            msg.Subject = title;
            //msg.TextBody = body;
            msg.HTMLBody = body;    //html文本
            msg.CC = cc;
            msg.AddAttachment(file);
            OverAll(msg, from);
        }

        #endregion

        #region 通用方法
        public static void OverAll(Message msg, string from)
        {

            string passWord = "passWord";

            string server = "192.168.0.30";

            CDO.IConfiguration iConfig = msg.Configuration;

            ADODB.Fields fields = iConfig.Fields;

            fields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2;

            fields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value = from;

            fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value = passWord;

            fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value = 0;

            fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = server;

            //fields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value = ;

            /*其中, fields的这五个属性是它所有可选属性的最紧的一个子集, 即,这五个属性是缺一不可的, 其它的属性还有:
              http://schemas.microsoft.com/cdo/configuration/smtpaccountname
              http://schemas.microsoft.com/cdo/configuration/smtpuserreplyemailaddress
              http://schemas.microsoft.com/cdo/configuration/sendusername
              等.
             */

            fields.Update();

            try
            {
                msg.Send();
                msg = null;
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }
        #endregion
    }
}
